package com.wing.common.util;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
	public static String KEY_YBL_YIYUE="ybl_yiyue";//已知能做什么key
	/**
	 * 设置cookie
	 * @param response
	 * @param key
	 * @param value
	 * @param time
	 * @throws IOException
	 */
	public static void setCookie(HttpServletResponse response,String key, String value,int time){
		Cookie cookie=new Cookie(key, value); 
		cookie.setMaxAge(time); 
		response.addCookie(cookie); 
	}
	
	/**
	 * 根据key值获取cookie的value
	 * @param request
	 * @param key
	 * @return
	 * @throws IOException
	 */
	public static String getCookie(HttpServletRequest request,String key) {
		Cookie cookies[]=request.getCookies(); 
		String svalue=null; 
		for(int i=0;i<cookies.length;i++){ 
			Cookie sCookie=cookies[i]; 
			if(key.equals(sCookie.getName())){
				svalue=sCookie.getValue();
			}
		}
		return svalue;
	}

	
}
